#include <iostream>
#include "Library.h"
#include <string>
#include <vector>
BookRecord::BookRecord(string& inid, string& intitle, string &inaufirstname, string &inaulastname, int& inyear, int& incopiesnum,int &z)
	:id(inid),title(intitle),aufirstname(inaufirstname),aulastname(inaulastname),year(inyear),copiesnum(incopiesnum),availnum(z)
{
	if (inid[0] < 'A' || inid[0] > 'Z' || inyear <= 1000 || inyear >= 3000 || inyear>=10000)
	{
		cout << "error message!program stop!" << endl;
		abort();
	}
}
string BookRecord::returnid()
{
	return id;
}
void BookRecord::getavailnum(int i)
{
	availnum = copiesnum - i;
}
int BookRecord::returnavailnum()
{
	return availnum;
}
void BookRecord::displaybookdata()const
{
	cout << "Book ID:" << id << '\n' << "Title:" << title << '\n' << "Author:" << aufirstname << " " << aulastname << '\n' << "Year published:" << year << '\n' << "Total number of copies:"
		<< copiesnum << '\n' << "Number available for loan:" << availnum << endl;
}
Borrower::Borrower(string &inid, string &infirstname, string &inlastname,int &inloanbook, vector<string>bookid)
	:bid(inid),bfirstname(infirstname),blastname(inlastname),loanbook(inloanbook),loanid(bookid)
{	
	
	if (bid[5] != '\0')
	{
		cout << "error message!program stop!" << endl;
		abort();
	}
}
void Borrower::displayborrowerdata()const
{
	cout << "Borrower ID:" << bid << "\n" << "Name:" << bfirstname << " " << blastname << '\n' << "Number of loaned books:" << loanbook << '\n' << "IDs of books on loan:";
	for (int i = 0; i < loanbook; i++)
	{
		cout << loanid[i] << " ";
	}
	cout << endl;
}
void Catalogue::getcatadata(BookRecord &book, vector<Borrower>&togetavail)
{
	int counter = 0;
	for (int t = 0; t < togetavail.size(); t++)
	{
		for (int n = 0; n < togetavail[t].returnloanbook(); n++)
		{
			if (togetavail[t].loanid[n] == book.returnid()) { counter++; }
		}
		bookloannum += togetavail[t].returnloanbook();
	}
	book.getavailnum(counter);
	recordbook.push_back(book);
}
void Catalogue::displaycatadata()const
{
	for (int i = 0; i<recordbook.size(); i++)
	{
		cout << "Book Record " << i << '\n' << "===============" << endl;
		recordbook[i].displaybookdata();
	}
}
void Library::getbookdata()
{
	int classnum = 0;
	int z = 0;
	cin >> classnum;
		for (int i = 0; i < classnum; i++)
		{
			string inid; string intitle; string infirstname; string inlastname;
			int inyear = 0, incopiesnum = 0;
			cin >> inid;
			cin >> intitle;
			cin >> infirstname;
			cin >> inlastname;
			cin >> inyear;
			cin >> incopiesnum;
			BookRecord book(inid, intitle, infirstname, inlastname, inyear, incopiesnum,z);
			bookcatalogue.getcatadata(book, borr);
			totalavail += book.returnavailnum();
		}
	
}
void Library::getborrdata()
{
	cin >> borrnum;
	
		for (int i = 0; i < borrnum; i++)
		{
			string inid; string infirstname; string inlastname; string bookid;
			int inloanbook;
			vector<string>inbookid;
			cin >> inid;
			cin >> infirstname;
			cin >> inlastname;
			cin >> inloanbook;
			cout << 2;
			loanbooknum += inloanbook;
			for (int t = 0; t < inloanbook; t++)
			{
				cin >> bookid;
				inbookid.push_back(bookid);
			}
			Borrower borrow(inid, infirstname, inlastname, inloanbook, inbookid);
			borr.push_back(borrow);
		}
	
	
}
void Library::displaybook()
{
	cout << "Total number of books on loan:" << loanbooknum << '\n' << "Total number of books in catalogue: " << totalavail << endl;
	bookcatalogue.displaycatadata();
}
void Library::displayborrower()
{
	for (int i = 0; i < borr.size(); i++)
	{
		cout << "Borrower " << i << '\n' << "===============" << '\n';
		borr[i].displayborrowerdata();
	}
}